//
//  JJRNFileOperation.m
//  ClaimPlatform_RN
//
//  Created by woshiku on 2018/8/22.
//  Copyright © 2018年 jj. All rights reserved.
//

#import "JJRNFileOperation.h"

@implementation JJRNFileOperation

+(NSString*)getLocalRNBundleFile{
    NSString * docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *rnBundleVersionPath=[NSString stringWithFormat:@"%@/\%@",docsdir,BUNDLE_VERSION_PLIST];
    return rnBundleVersionPath;
}

+(NSString*)getLocalRNBundleVersion{
    NSString *rnBundleVersionPath=[self getLocalRNBundleFile];
    NSDictionary *rnBundleVersionDic = [[NSDictionary alloc] initWithContentsOfFile:rnBundleVersionPath];
    NSString *rnBundleVersion=@"";//RNBundleVersion 默认为空字符串
    if (rnBundleVersionDic) {
        //本地存在RNBundleVersion信息，读取RNbundle的版本信息
        rnBundleVersion=[rnBundleVersionDic objectForKey:RNBUNDLEVERSIONKEY];
    }
    return rnBundleVersion;
}

+(NSString*)getLocalRNBundleMD5File{
    NSString *docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *rnBundleVersionPath=[NSString stringWithFormat:@"%@/\%@",docsdir,BUNDLE_MD5_PLIST];
    return rnBundleVersionPath;
}

+(NSString*)getRNBundleMD5Value:(NSString*)rnVersion{
    NSString *rnBundleMD5Path=[self getLocalRNBundleMD5File];
    NSMutableDictionary *rnBundleMD5Dic = [[NSMutableDictionary alloc] initWithContentsOfFile:rnBundleMD5Path];
    NSString *bundleMD5=@"";//bundleMD5 默认为空字符串
    if (rnBundleMD5Dic) {
        //本地存在RNBundleVersion信息，读取RNbundle的版本信息
        bundleMD5=[rnBundleMD5Dic objectForKey:[NSString stringWithFormat:@"%@%@",RNBUNDLEFLODERPRE,rnVersion]];
    }
    return JJ_NSSTRING_NOT_NIL(bundleMD5);
}
+(void)storeRNBundleMD5ToFile:(NSString*)rnVersion MD5:(NSString*)md5{
    NSString *rnBundleMD5Path=[self getLocalRNBundleMD5File];
    
    NSMutableDictionary *rnBundleMD5Dic = [[NSMutableDictionary alloc] initWithContentsOfFile:rnBundleMD5Path];
    if(!rnBundleMD5Dic){
        rnBundleMD5Dic=[[NSMutableDictionary alloc]initWithCapacity:0];
    }
    [rnBundleMD5Dic setObject:[md5 uppercaseString] forKey:[NSString stringWithFormat:@"%@%@",RNBUNDLEFLODERPRE,rnVersion]];
    [self storeFile:rnBundleMD5Dic fileName:[self getLocalRNBundleMD5File]];
}
+(NSString *)getBundleFloderPath{
    NSString * docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *rnHotUpdateFolderPath = [docsdir stringByAppendingPathComponent:RNHOTUPDATEFLODER];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = NO;
    // fileExistsAtPath 判断一个文件或目录是否有效，isDirectory判断是否一个目录
    BOOL existed = [fileManager fileExistsAtPath:rnHotUpdateFolderPath isDirectory:&isDir];
    
    if (!(isDir && existed)) {
        // 在Document目录下创建一个archiver目录
        [fileManager createDirectoryAtPath:rnHotUpdateFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return rnHotUpdateFolderPath;
}
+(NSString*)getRnBundleTempPath{
    
    NSString * docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *rnBundleDownTempPath = [docsdir stringByAppendingPathComponent:DOWNLOADRNBUNDLE];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL isDir = NO;
    // fileExistsAtPath 判断一个文件或目录是否有效，isDirectory判断是否一个目录
    BOOL existed = [fileManager fileExistsAtPath:rnBundleDownTempPath isDirectory:&isDir];
    
    if (!(isDir && existed)) {
        // 在Document目录下创建一个archiver目录
        [fileManager createDirectoryAtPath:rnBundleDownTempPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return rnBundleDownTempPath;
}

+(void)deleteLocalFile:(NSString*)file{
    BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:file];
    if(fileExist){//如果存在就删除
        [[NSFileManager defaultManager]removeItemAtPath:file error:nil];
    }
}

+(BOOL)localFileIsExit:(NSString*)file{
    BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:file];
    return fileExist;
}

+(void)storeFile:(NSMutableDictionary*)dic fileName:(NSString*)fileFullPath{
    BOOL configureHave=[[NSFileManager defaultManager] fileExistsAtPath:fileFullPath];
    if (configureHave) {
        BOOL blDele= [[NSFileManager defaultManager] removeItemAtPath:fileFullPath error:nil];
        if (blDele) {
            ;
        }else {
            ;
        }
        BOOL write=[dic writeToFile:fileFullPath atomically:YES];
        
    }else{
        [dic writeToFile:fileFullPath atomically:YES];
    }
}
+(BOOL)moveFile:(NSString*)currentPath DestiFile:(NSString*)toPath{
    // 移动文件
    BOOL isMove = [[NSFileManager defaultManager]  moveItemAtPath:currentPath toPath:toPath error:nil];
    return isMove;
}
@end
